<template>
  <section>
    <!--工具条，就是高级查询的那个框-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="配置名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getQueryDepartments">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="this.pageInfo.dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="105">
      </el-table-column>
      <el-table-column type="index" label="序号" width="160">
      </el-table-column>
      <el-table-column prop="sn" label="sn" width="250">
      </el-table-column>
      <el-table-column prop="title" label="title" width="280">
      </el-table-column>
      <el-table-column prop="status" label="状态" min-width="80">
        <template scope="scope">
          <span v-if="scope.row.status == 0" style="color: red;">禁用</span>
          <span v-if="scope.row.status == 1" style="color:green">启用</span>
        </template>
      </el-table-column>
      <el-table-column prop="intro" label="描述" width="280">
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="success" size="small" @click="handleCheckInfo(scope.$index, scope.row)">查看明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-- 批量删除和分页 -->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="this.query.pageSize" :total="this.pageInfo.total" style="float:right;">
            </el-pagination>-->

      <!--      layout 属性加上什么，底部就会出现什么-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="this.query.currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="10"
          style="float:right"
          layout="total,sizes, prev, pager, next,jumper"
          :total="this.pageInfo.total">
      </el-pagination>
    </el-col>


    <!--新增/修改界面-->
    <el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="sn" prop="key">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="title" prop="value">
          <el-input v-model="saveForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="intro">
          <el-input v-model="saveForm.intro" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.status">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" >提交</el-button>
      </div>
    </el-dialog>

<!--    查看明细弹窗-->
    <el-dialog title="查看明细" :visible.sync="infoFormVisible" :close-on-click-modal="false">
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="itemQuery">
          <el-form-item>
            <el-input v-model="itemQuery.keyword" placeholder="明细"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="getQueryItems">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-table :data="this.itemPageInfo.dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="index" label="序号" width="160">
        </el-table-column>
        <el-table-column prop="title" label="title" width="150">
        </el-table-column>
        <el-table-column prop="value" label="value" width="150">
        </el-table-column>
        <el-table-column prop="sequence" label="sequence" width="150">
        </el-table-column>
        <el-table-column prop="parent.title" label="parent" width="150">
        </el-table-column>
        <el-table-column prop="intro" label="描述" width="180">
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleItemSizeChange"
          @current-change="handleItemCurrentChange"
          :current-page.sync="this.itemQuery.currentPage"
          :page-sizes="[5, 10]"
          :page-size="10"
          style="float:right"
          layout="total,sizes, prev, pager, next,jumper"
          :total="this.itemPageInfo.total">
      </el-pagination>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'

export default {
  data() {
    return {
      query: {  // 定义高级查询的 属性，也是要通过请求带给后端的
        keyword: '',
        currentPage:1,
        pageSize:10
      },
      pageInfo:{
        dataList: [],
        total: 0,
      },
      page: 1,
      listLoading: false,
      sels: [],//列表选中列


      saveFormVisible: false,//新增界面是否显示
      saveFormRules: {

      },
      //新增界面数据
      saveForm: {
        id: null,
        sn: " ",
        intro: "",
        title:"",
        status: ""
      },
      // 明细弹窗
      infoFormVisible:false,
      itemQuery:{
        keyword: '',
        currentPage:1,
        pageSize:10,
        id:null
      },
      itemPageInfo:{
        dataList: [],
        total: 0,
      }
    }
  },
  methods: {
    //查看明细
    handleCheckInfo(index, row){
      this.infoFormVisible=true
      this.itemQuery.id=row.id
      this.getCheckInfo()
    },
    getCheckInfo(){
      this.$http.put("/dictionaryitem",this.itemQuery)
          .then(result=>{
            result=result.data;
            if(result.success){
              this.$message({message: '查询成功',type: 'success'});
              this.itemPageInfo =result.ajaxObj;
            }else {
              this.$message({message: '查询失败',type: 'error'});
            }
          })
          .catch(result=>{
            this.$message({message: '网络异常，查询列表失败',type: 'error'});
          })
    },
    // 调整明细每页数据条数
    handleItemSizeChange(val){
      this.itemQuery.pageSize=val;
      this.getCheckInfo()
    },

    handleItemCurrentChange(val) {
      this.itemQuery.currentPage=val;
      this.getCheckInfo() // getDictionarys 方法中的 axios 请求会自己来这里找
    },
    // 点击查询按钮
    getQueryItems(){
      this.itemQuery.currentPage=1;
      this.getCheckInfo()
    },


    // 点击查询按钮
    getQueryDepartments(){
      this.query.currentPage=1;
      this.getDictionarys()
    },
    // 调整每页数据条数
    handleSizeChange(val){
      this.query.pageSize=val;
      this.getDictionarys()
    },

    handleCurrentChange(val) {
      this.query.currentPage=val;
      this.getDictionarys() // getDictionarys 方法中的 axios 请求会自己来这里找
    },


    //获取配置列表
    getDictionarys() {
      this.listLoading = true;
      // 发请求
      this.$http.put("/dictionary",this.query)
          .then(result=>{
            result=result.data;
            if(result.success){
              this.$message({message: '查询成功',type: 'success'});
              this.listLoading = false;
              this.pageInfo =result.ajaxObj;
            }else {
              this.$message({message: '查询失败',type: 'error'});
            }
          })
          .catch(result=>{
            this.$message({message: '网络异常，查询列表失败',type: 'error'});
          })
    },


    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { id: row.id };
        this.$http.delete("/dictionary/"+row.id)
            .then((result) => {
              result=result.data;
              if(result.success){
                this.$message({message: '删除成功',type: 'success'});
                this.listLoading = false;
                this.query.currentPage=1;
                this.getDictionarys()
              }else {
                this.$message({message: '删除失败',type: 'error'});
              }
            }).catch();
      }).catch(() => {

      });
    },
    //显示编辑界面的弹窗 --- 下拉框的回显和级联框的回显(掐头去尾，头是空字符串，尾是自己)
    handleEdit: function (index, row) {
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);   // 回显

    },
    //显示新增界面的弹窗
    handleAdd: function () {
      this.saveFormVisible = true;
      this.saveForm = {
        id: null,
        key: " ",
        intro: "",
        value:"",
        type: ""
      }
    },

    //新增的提交按钮，上级配置的级联框取数组的最后一个
    addSubmit: function () {
      this.$refs.saveForm.validate((valid) => {   // valid 就是规则的返回值，规则通过了才会有返回值
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.saveForm);  // JSON 格式的转换
            // 发请求
            this.$http.post("/dictionary",para)
                .then((result) => {
                  result=result.data;
                  if(result.success){
                    this.$message({message: '保存成功',type: 'success'});
                    this.addLoading=false
                    this.saveFormVisible=false
                    this.getDictionarys()
                  }else {
                    this.$message({message: '保存失败',type: 'error'});
                  }
                })
                .catch(result => {
                  this.$message({message: '网络异常，请稍后访问',type: 'danger'});
                  console.log(result)
                })
          });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/dictionary",ids)
            .then((result) => {
              result=result.data;
              if(result.success){
                this.$message({message: '批量删除成功',type: 'success'});
                this.listLoading = false;
                this.query.currentPage=1;
                this.getDictionarys()
              }else {
                this.$message({message: '批量删除失败',type: 'error'});
              }
            })
            .catch(result => {

            })
      }).catch(()=>{})
    }
  },


  mounted() {
    this.getDictionarys();
  }
}

</script>

<style scoped>

</style>